package com.party.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.party.common.entity.MyPage;
import com.party.common.utils.PageUtil;
import com.party.entity.PbResource;
import com.party.entity.PbUserResource;
import com.party.entity.PbUserResourceHistory;
import com.party.entity.dto.QueryResourceDto;
import com.party.mapper.PbUserResourceHistoryMapper;
import com.party.service.PbResourceService;
import com.party.service.PbUserResourceHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.loader.ResourceEntry;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 资源 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-10-31
 */
@Service
public class PbUserResourceHistoryServiceImpl extends ServiceImpl<PbUserResourceHistoryMapper, PbUserResourceHistory> implements PbUserResourceHistoryService {

    @Resource
    private PbResourceService pbResourceService;

    @Override
    public MyPage<PbUserResourceHistory> queryPage(QueryResourceDto dto) {
        LambdaQueryWrapper<PbUserResourceHistory> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(dto.getResourceName())) {
            wrapper.like(PbUserResourceHistory::getResourceName, dto.getResourceName());
        }
        if(ObjectUtil.isNotEmpty(dto.getOpenId())){
            wrapper.like(PbUserResourceHistory::getOpenId,dto.getOpenId());
        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        wrapper.orderByDesc(PbUserResourceHistory::getCreateTime);
        List<PbUserResourceHistory> userResourceHistories = this.list(wrapper);
        for (int i = 0; i < userResourceHistories.size(); i++) {
            userResourceHistories.get(i).setId(userResourceHistories.get(i).getResourceId());
        }
//        if(userResourceHistories.size()<10){
//            if(userResourceHistories==null){
//                userResourceHistories=new ArrayList<>();
//            }
//            LambdaQueryWrapper<PbResource> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.orderByDesc(PbResource::getCreateTime);
//            wrapper1.last("limit "+(10-userResourceHistories.size()));
//            List<PbResource> resourceList=pbResourceService.list(wrapper1);
//            for(PbResource pbResource:resourceList){
//                PbUserResourceHistory pbUserResourceHistory=new PbUserResourceHistory();
//                pbUserResourceHistory.setOpenId(dto.getOpenId());
//                BeanUtils.copyProperties(pbResource,pbUserResourceHistory);
//                userResourceHistories.add(pbUserResourceHistory);
//            }
//        }
        PageInfo<PbUserResourceHistory> info = new PageInfo<>(userResourceHistories);
        return PageUtil.PageInfo2MyPage(info);
    }
}
